package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obKho;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class TK_ChonKho extends Activity {

	MyCustomAdapter dataAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tk_chonkho);

		ListView lvKho = (ListView) findViewById(R.id.lvKho);
		Button btYes = (Button) findViewById(R.id.btYes);
		Button btCancel = (Button) findViewById(R.id.btCancel);

		ArrayList<obKho> khos = getKho(Global.UserName);
		dataAdapter = new MyCustomAdapter(this, R.layout.tk_chonkho, khos);
		// Assign adapter to ListView
		lvKho.setAdapter(dataAdapter);

		btYes.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strListKho = "";
				ArrayList<obKho> stateList = dataAdapter.stateList;
				for (int i = 0; i < stateList.size(); i++) {
					obKho state = stateList.get(i);
					if (state.getIsChoose()) {
						strListKho += "," + state.getMaKho();
					}
				}
				if (!strListKho.equals(""))
					strListKho = strListKho.substring(1);
				Toast.makeText(getApplicationContext(), strListKho,
						Toast.LENGTH_LONG).show();
				Intent i = new Intent();
				Bundle b = new Bundle();
				b.putString("strListKho", strListKho);
				i.putExtras(b);
				setResult(RESULT_OK, i);
				finish();
			}
		});

		btCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setResult(RESULT_CANCELED);
				finish();
			}
		});

	}

	public ArrayList<obKho> getKho(String strUserName) {
		ArrayList<obKho> khos = new ArrayList<obKho>();
		String sql = "spKho_User '" + strUserName + "'";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			obKho obkhoFirst = new obKho();
				obkhoFirst.setMaKho("");
				obkhoFirst.setTenKho("Tất cả");
			khos.add(obkhoFirst);
			while (rs.next()) {
				obKho obkho = new obKho();
				if (rs.getString("MSK") != null)
					obkho.setMaKho(rs.getString("MSK").toString());
				if (rs.getString("Tenkho") != null)
					obkho.setTenKho(rs.getString("Tenkho").toString());
				khos.add(obkho);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return khos;
	}

	private class MyCustomAdapter extends ArrayAdapter<obKho> {

		private ArrayList<obKho> stateList;

		public MyCustomAdapter(Context context, int textViewResourceId,

		ArrayList<obKho> stateList) {
			super(context, textViewResourceId, stateList);
			this.stateList = new ArrayList<obKho>();
			this.stateList.addAll(stateList);
		}

		private class ViewHolder {
			TextView code;
			CheckBox name;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;

			Log.v("ConvertView", String.valueOf(position));

			if (convertView == null) {

				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = vi.inflate(R.layout.layout_chonkho, null);

				holder = new ViewHolder();
				holder.code = (TextView) convertView.findViewById(R.id.tvKho);
				holder.name = (CheckBox) convertView.findViewById(R.id.ckbKho);

				convertView.setTag(holder);

				holder.name.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						CheckBox cb = (CheckBox) v;
						obKho _state = (obKho) cb.getTag();
						// Toast.makeText(
						// getApplicationContext(),
						// "Checkbox: " + cb.getText() + " -> "
						// + cb.isChecked(), Toast.LENGTH_LONG)
						// .show();
						_state.setIsChoose(cb.isChecked());
					}
				});

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			obKho state = stateList.get(position);

			holder.code.setText(" (" + state.getMaKho() + ")");
			holder.name.setText(state.getTenKho());
			holder.name.setChecked(state.getIsChoose());

			holder.name.setTag(state);

			return convertView;
		}

	}
	
	//get Kho default 
	public static String getKhoDefault(String user)
	{
		String t = "";
		String sql = " select case when (Khomacdinh is null) then '' else Khomacdinh end from tblConfig "
				+ " where [USER] = '"+user+"'";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			if(rs.next())
			{
				t= rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}

}
